import Vue from 'vue'
import VueRouter from 'vue-router'

import MainView from '../views/MainView.vue'
import HomeView from '../views/HomeView.vue'

// 登录页
import LoginView from '../views/LoginView.vue'
// 注册页
import RegisterView from '../views/RegisterView.vue'

// 线索页
import clueView from '../views/MarkViews/clueView.vue'
//活动管理
import activityView from '../views/MarkViews/activityView.vue'
// 渠道管理
import channelView from '../views/MarkViews/channelView.vue'

// 客户管理
// 客户管理
import manageUser from '../views/CustomerViews/ManageUser.vue'
import detailUser from '../views/CustomerViews/DetailUser.vue'

// 车辆管理
import vehicleView from '../views/InventoryViews/vehicleView.vue'
// 排队库管理
import queueView from '../views/InventoryViews/queueView.vue'

// 合同管理
import contractView from '../views/FinanceViews/contractView.vue'
// 订单管理
import orderView from '../views/FinanceViews/orderView.vue'
// 订单详情
import orderDetails from '../views/FinanceViews/orderDetails.vue'
//添加线索
import TianJia from '../views/MarkViews/tianjia/TianJia.vue'
// 添加活动
import TianJiaHuoDong from '../views/MarkViews/tianjia/TianJiaHuoDong.vue'
//线索编辑
import BianJi from "@/views/MarkViews/bianji/BianJi.vue"
// 活动编辑
import HuoDongBianJi from "@/views/MarkViews/bianji/HuoDongBianJi.vue"
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        component: MainView,
        redirect: '/home',
        children: [
            { path: '/home', component: HomeView },
            { path: '/clue', component: clueView },
            { path: '/activity', component: activityView },
            { path: '/channel', component: channelView },
            { path: '/customer', component: manageUser },
            { path: '/detailUser', component: detailUser },
            { path: '/vehicle', component: vehicleView },
            { path: '/queue', component: queueView },
            { path: '/contract', component: contractView },
            { path: '/order', component: orderView },
            { path: '/orderdetails', component: orderDetails },

            { path: '/tianjia', component: TianJia },
            { path: '/tianjiahuodong', component: TianJiaHuoDong },
            { path: '/bianji', component: BianJi },
            { path: '/huodongbianji', component: HuoDongBianJi },
        ]
    },
    {
        path: '/login',
        component: LoginView,
    },
    {
        path: '/register',
        component: RegisterView,
    }

]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // 如果访问的是登录界面则直接放行
    if (to.path === '/login') return next()
    if (to.path === '/register') return next()
    //获取用户页面token信息
    let token = window.sessionStorage.getItem('token')
    //如果token数据为null则跳转到指定路径
    if (!token) {
        return next("/login")
    }
    next()
})



export default router